﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.Reflection;
using System.IO;
using System.Windows.Threading;
using System.Threading;

namespace InscriberControlClient
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        Window2 w2 = new Window2();
        Window3 w3 = new Window3();
        Window5 w5 = new Window5();
        Window6 w6 = new Window6();
        
        Window4 w4 = null;
        public static bool startedRL = false;
        public static bool startedLD = false;
        public static bool auto = false;
        public int timer = 0;
        public static bool isMove = false;
        DispatcherTimer dispatcherTimer = new DispatcherTimer();

        ListView listView;

        // delegate for GetPosition function of DragEventArgs and
        // MouseButtonEventArgs event argument objects. This delegate is used to reuse the code
        // for processing both types of events.

        public Window1()
        {
            InitializeComponent();
            Functionality.mainW = this;          
            Functionality.readXMLParameters();
            startedRL = Functionality.getStatusRL(AButton,checkBox6);
            startedLD = Functionality.getStatusLD(this,button4);
            auto = Functionality.getStatusAuto(this, button6, button7,button4);
            w4=new Window4(this);

            ContextMenu mainMenu = new ContextMenu();
            mainMenu.Height = 30;
            mainMenu.Width = 150;

            MenuItem item1 = new MenuItem();
            item1.Width = 150;
            item1.Header = "Playlist Langas";
            item1.Click += new RoutedEventHandler(MenuItem1_Click);
            mainMenu.Items.Add(item1);
                        
            this.ContextMenu = mainMenu;


            ContextMenu listviewMenu = new ContextMenu();
            listviewMenu.Height = 30;
            listviewMenu.Width = 150;

            MenuItem Litem1 = new MenuItem();
            Litem1.Width = 150;
            Litem1.Header = "Šalinti";
            Litem1.Click += new RoutedEventHandler(ListViewMenuItem1_Click);
            listviewMenu.Items.Add(Litem1);

            listView1.ContextMenu = listviewMenu;

           
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);

            Functionality.getVedejai(comboBox1);
            //Functionality.setChanelMix();
            //ImageBrush myBrush = new ImageBrush();
           // myBrush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/stop.jpg", UriKind.Absolute));
            //this.Background = myBrush;

           // Assembly asm = Assembly.GetExecutingAssembly();
          //  Stream resStream = asm.GetManifestResourceStream("InscriberControlClient.Properties.Resources.stop");
           // Bitmap pic = new Bitmap(resStream);
           // image1.Source = (ImageSource)Application.Current.FindResource("stop"); 
         // BitmapImage fonas = new BitmapImage();
         // fonas.BeginInit();
        //  fonas.UriSource = new Uri(@"/InscriberControlClient;component/Images/stop.JPG", UriKind.Relative);//new Uri("pack://application:,,,/InscriberControlClient;component/Resources/stop.JPG");
         // fonas.EndInit();
         // image1.Source = fonas;
          //image1.Source = BitmapFrame.Create(Application.GetResourceStream(new Uri("Images/stop.JPG", UriKind.RelativeOrAbsolute)).Stream);
        }

        void MenuItem1_Click(object sender, EventArgs e)
        {
            w4.Show();
        }

        void ListViewMenuItem1_Click(object sender, EventArgs e)
        {
            int n = listView1.SelectedItems.Count;
            for (int i = 0; i < n; i++)
            {
                listView1.Items.Remove(listView1.SelectedItems[0]);
            }
        }


        private void AButton_Click(object sender, RoutedEventArgs e)
        {
            if (!startedRL)
            {
                Functionality.startRL(checkBox6);
                startedRL = Functionality.getStatusRL(AButton,checkBox6);
            }
            else
            {
                Functionality.stopRL();
                startedRL = Functionality.getStatusRL(AButton,checkBox6);
            }

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            w2.Show();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            w3.Show();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            w2.Close();
            w3.Close();
            w5.Close();
            w6.Close();
            w4.Hide();
            w4.Close();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Left = this.Left + 100;
            w2.Left = button2.Margin.Left + this.Left;
            w2.Top = this.Top + this.Height - button2.Margin.Bottom - button2.Height - w2.Height - 10;
            w3.Left = this.Width - button1.Margin.Right - w3.Width + this.Left;
            w3.Top = this.Top + this.Height - button1.Margin.Bottom - button2.Height - w3.Height - 10;
            w5.Left = this.Left + button3.Margin.Left + button3.Width +10 ;
            w5.Top = this.Top  + button3.Margin.Top;
            w6.Left = this.Left + this.Width - button4.Margin.Right - button4.Width - 10 - w6.Width;
            w6.Top = this.Top + button4.Margin.Top-45;
            

            w4.Height = this.Height;
            w4.Width = 200;
            w4.Top = this.Top;
            w4.Left = this.Left - 200;
            w4.Show();
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            w5.Show();
        }

        public void RenewChildWindows(string news_id,string report_id)
        {
            //MessageBox.Show(news_id + "----" + report_id);
            w2.peopleListBox.DataContext = Functionality.getLydai(report_id);
            w2.Show();
            w2.Show();
            w3.peopleListBox.DataContext = Functionality.getTitrai(report_id);
            w3.Show();
            w3.Show();
            w5.peopleListBox.DataContext = Functionality.getLogo(report_id);
            w5.Show();
            w5.Show();
            //w6.peopleListBox.DataContext = Functionality.getAnonsai(report_id);
            //w6.Show();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            if (!startedLD)
            {
                Functionality.startedLD = true;
                if (auto)
                {
                    Functionality.startLDloop(Functionality.LDid, true);
                }
                else
                {
                    Functionality.startLDloop(Functionality.LDid, false);
                }
                startedLD = Functionality.getStatusLD(this,button4);
                dispatcherTimer.Start();
            }
            else
            {
                Functionality.startedLD = false;
                Functionality.stopped=true;
                ResetTimer(false);
            }

        }

        public void ResetTimer(bool same)
        {
            timer = 0;
            dispatcherTimer.Stop();
            label1.Content = "00:00";
            Functionality.resetLD(same);
            startedLD = Functionality.getStatusLD(this, button4);
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            w6.Show();
        }

        private void button6_Click(object sender, RoutedEventArgs e)
        {
            if (!startedLD)
            {
                Functionality.updateAuto(1);
                auto = Functionality.getStatusAuto(this, button6, button7, button4);
            }
            else
            {
                ///MessageBox.Show("Sustabdykite timer'į !");
            }
        }

        private void button7_Click(object sender, RoutedEventArgs e)
        {
            if (!startedLD)
            {
            Functionality.updateAuto(0);
            auto = Functionality.getStatusAuto(this, button6, button7,button4);
            }
            else
            {
               /// MessageBox.Show("Sustabdykite timer'į !");
            }
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            timer++;
            string min = (timer / 60).ToString();
            if (min.Length==1){min="0"+min;}
            string sec=(timer % 60).ToString();
            if (sec.Length==1){sec="0"+sec;}
            label1.Content = min + ":" + sec;
          //  CommandManager.InvalidateRequerySuggested();

        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Functionality.createVT(comboBox1);
        }

        private void listView1_Drop(object sender, DragEventArgs e)
        {
            string tag=Functionality.si.news_id+"_"+Functionality.si.rep_id;
            ListViewItem li = new ListViewItem() { Content = Functionality.si.repname, Tag=tag};
            listView1.Items.Add(li);
        }

        private void listView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListView list = (ListView)sender;
            ListViewItem temp = ((ListViewItem)list.SelectedItem);
            try
            {
                string news_id = temp.Tag.ToString().Split('_')[0];
                string reports_id = temp.Tag.ToString().Split('_')[1];
                RenewChildWindows(news_id, reports_id);
                //MessageBox.Show(news_id + "----" + reports_id);
            }
            catch (Exception ex) { }
        }

        private void button8_Click(object sender, RoutedEventArgs e)
        {
            if (checkBox1.IsChecked==true)
               Functionality.CleanChanel("A");
            if (checkBox2.IsChecked == true)
                Functionality.CleanChanel("B");
            if (checkBox3.IsChecked == true)
                Functionality.CleanChanel("C");
            if (checkBox4.IsChecked == true)
                Functionality.CleanChanel("D");
            if (checkBox5.IsChecked == true)
                Functionality.CleanChanel("E");
        }

        private void checkBox6_Checked(object sender, RoutedEventArgs e)
        {

        }

        private void button9_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Media.Color c = Colors.Red;
            button9.Background = new SolidColorBrush(c);
            
            try
            {
                for (int i = 0; i < listView1.SelectedItems.Count;i++ )
                {
                    ListViewItem temp = ((ListViewItem)listView1.SelectedItems[i]);
                    string news_id = temp.Tag.ToString().Split('_')[0];
                    string reports_id = temp.Tag.ToString().Split('_')[1];
                    Functionality.generateLydaiTemplates(reports_id);
                    Functionality.generateTitraiTemplates(reports_id);
                    Functionality.generateLogoTemplates(reports_id);
                    Functionality.generateAnonsaiTemplates(reports_id);
                    //Functionality.generateValiutos();
                    Thread.Sleep(3000);
                    c = Colors.Green;
                    button9.Background = new SolidColorBrush(c);
                }
            }
            catch (Exception ex) { MessageBox.Show("generavimo klaida! :"+ex.Message); }

        }

        private void button10_Click(object sender, RoutedEventArgs e)
        {
            ResetTimer(false);
        }

        private void button11_Click(object sender, RoutedEventArgs e)
        {
            Functionality.StartFonas(checkBox7);
        }

        private void button12_Click(object sender, RoutedEventArgs e)
        {
            Window8 w8 = new Window8();
            if (w8.ShowDialog() == true)
            {
                Functionality.getVedejai(comboBox1);
            }
        }

        private void button14_Click(object sender, RoutedEventArgs e)
        {
            int location = listView1.SelectedIndex;
            if (location < listView1.Items.Count - 1)
            {
                object rememberMe = listView1.SelectedItem;
                listView1.Items.RemoveAt(location);
                listView1.Items.Insert(location + 1, rememberMe);
                listView1.SelectedIndex = location + 1;
            }
        }

        private void button13_Click(object sender, RoutedEventArgs e)
        {
            int location = listView1.SelectedIndex;
            if (location > 0)
            {
                object rememberMe = listView1.SelectedItem;
                listView1.Items.RemoveAt(location);
                listView1.Items.Insert(location - 1, rememberMe);
                listView1.SelectedIndex = location - 1;
            }
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void Window_MouseLeave(object sender, MouseEventArgs e)
        {
            isMove = false;
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
          /*  isMove = true;
            Functionality.lastPosition = Mouse.GetPosition(this);*/
        }

        private void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isMove = false;
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
           /* if (isMove)
            {
                    System.Windows.Point pt = Mouse.GetPosition(this);
                    Functionality.DragMoveNew(this, e, pt);
                    w4.Top = this.Top;
                    w4.Left = this.Left - 200;
            }*/
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

        private void button15_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (MessageBox.Show("Ar tikrai trinti sugeneruotus failus?", "Trynimas", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    string[] files = Directory.GetFiles(@"D:\Output");
                    string digits = "0123456789";
                    foreach (string filen in files)
                    {
                        string file = System.IO.Path.GetFileNameWithoutExtension(filen);
                        if (file.Contains(".xml"))
                        {
                            File.Delete(filen);
                        }
                        if ((file[0] == 'L') && (file[1] == 'G') && (digits.Contains(file[2])))
                        {
                            File.Delete(filen);
                        }
                        if ((file[0] == 'L') && (file[1] == 'P') && (digits.Contains(file[2])))
                        {
                            File.Delete(filen);
                        }
                        if ((file[0] == 'L') && (digits.Contains(file[2])) && (digits.Contains(file[3])))
                        {
                            File.Delete(filen);
                        }
                        if ((file[0] == 'T') && (digits.Contains(file[2])) && (digits.Contains(file[3])))
                        {
                            File.Delete(filen);
                        }
                    }
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }

        private void button16_Click(object sender, RoutedEventArgs e)
        {
            Functionality.NetrukusLaidoje();
        }

        private void button17_Click(object sender, RoutedEventArgs e)
        {
            Functionality.ZiniosTrumpai();
        }

        private void Image_ImageFailed_1(object sender, ExceptionRoutedEventArgs e)
        {

        }

      
    }
}
